package com.tang.managementdemo.service.Impl;

import com.tang.managementdemo.exception.LoginNotExistException;
import com.tang.managementdemo.mapper.LoginMapper;
import com.tang.managementdemo.pojo.Employ;
import com.tang.managementdemo.pojo.LoginInfo;
import com.tang.managementdemo.service.LoginService;
import com.tang.managementdemo.util.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
@Slf4j
public class LoginServiceImpl implements LoginService {
    @Autowired
    private LoginMapper loginMapper;
    @Autowired
    private JwtUtils jwtUtils;
    @Override
    public LoginInfo login(String username, String password) {

        Employ loginInfo = loginMapper.login(username, password);
        if (loginInfo != null) {
            Map<String,Object> dataMap=new HashMap<>();
            dataMap.put("id",loginInfo.getId());
            dataMap.put("name",loginInfo.getName());
            dataMap.put("username",loginInfo.getUsername());

            System.out.println(dataMap.get("name")+"_____");
//            log.info("id"+loginInfo.getId()+"---------");
            log.info("name"+loginInfo.getName()+"---------");
            String jwt=jwtUtils.generateJwt(dataMap);
            System.out.println("jwt:"+jwt);
            System.out.println(jwtUtils.getHeader());
            return new LoginInfo(loginInfo.getId(),loginInfo.getUsername(),loginInfo.getPassword(),loginInfo.getName(),jwt);
        }
        throw new LoginNotExistException("用户名或密码错误");
    }
}
